package com.logistics.controller.tenant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.logistics.common.Result;
import com.logistics.dto.VehicleCreateRequest;
import com.logistics.entity.Vehicle;
import com.logistics.service.VehicleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 租户端 - 车辆管理控制器
 */
@RestController
@RequestMapping("/tenant/vehicle")
public class VehicleController {
    
    private final VehicleService vehicleService;
    
    public VehicleController(VehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }
    
    /**
     * 创建车辆
     */
    @PostMapping
    @PreAuthorize("hasAuthority('vehicle:create') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<Vehicle> createVehicle(@Validated @RequestBody VehicleCreateRequest request) {
        try {
            Vehicle vehicle = vehicleService.createVehicle(request);
            return Result.success("车辆创建成功", vehicle);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 车辆列表（分页）
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('vehicle:view') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<IPage<Vehicle>> getVehicleList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String vehicleStatus,
            @RequestParam(required = false) String keyword) {
        try {
            IPage<Vehicle> page = vehicleService.getVehicleList(current, size, vehicleStatus, keyword);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 车辆详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('vehicle:view') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<Vehicle> getVehicleById(@PathVariable Long id) {
        try {
            Vehicle vehicle = vehicleService.getVehicleById(id);
            if (vehicle == null) {
                return Result.error("车辆不存在");
            }
            return Result.success(vehicle);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新车辆
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('vehicle:edit') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<String> updateVehicle(@PathVariable Long id,
                                        @Validated @RequestBody VehicleCreateRequest request) {
        try {
            vehicleService.updateVehicle(id, request);
            return Result.success("车辆更新成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除车辆
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('vehicle:delete') or hasAnyRole('SUPER_ADMIN','SITE_ADMIN')")
    public Result<String> deleteVehicle(@PathVariable Long id) {
        try {
            vehicleService.deleteVehicle(id);
            return Result.success("车辆删除成功", null);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

